/**
 * 
 */
package com.ibuonline.search.solr.extend;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.SolrInputField;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.update.processor.UpdateRequestProcessor;

/**
 * @author Liuye
 * 
 */
public class SolrIndexContext {
	private SolrQueryRequest request;
	private SolrInputDocument document;
	private Map<String, Object> model = new HashMap<String, Object>();
	private String idName = "id";
	private UpdateRequestProcessor nextUpdateRequestProcessor;
	private boolean onlyExternal = true;

	public Map<String, Object> getModel() {
		return model;
	}

	/**
	 * @return the request
	 */
	public SolrQueryRequest getRequest() {
		return request;
	}

	/**
	 * @param request
	 *            the request to set
	 */
	public void setRequest(SolrQueryRequest request) {
		this.request = request;
	}

	/**
	 * @return the document
	 */
	public SolrInputDocument getDocument() {
		return document;
	}

	/**
	 * @param document
	 *            the document to set
	 */
	public void setDocument(SolrInputDocument document) {
		this.document = document;

	}

	/**
	 * @return the nextUpdateRequestProcessor
	 */
	public UpdateRequestProcessor getNextUpdateRequestProcessor() {
		return nextUpdateRequestProcessor;
	}

	/**
	 * @param nextUpdateRequestProcessor
	 *            the nextUpdateRequestProcessor to set
	 */
	public void setNextUpdateRequestProcessor(
			UpdateRequestProcessor nextUpdateRequestProcessor) {
		this.nextUpdateRequestProcessor = nextUpdateRequestProcessor;
	}

	/**
	 * @return the idName
	 */
	public String getIdName() {
		return idName;
	}

	/**
	 * @param idName
	 *            the idName to set
	 */
	public void setIdName(String idName) {
		this.idName = idName;
	}

	public void handleDocumentWithPrefix(String prefix) {
		String idFieldName = request.getSchema().getUniqueKeyField().getName();
		SolrInputField idInputField = document.getField(idFieldName);
		if (idInputField == null) {
			String msg = "the document missing uniqueKey field value.";
			throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, msg);
		}
		model.put(idName, idInputField.getValue());
		for (Map.Entry<String, SolrInputField> entry : document.entrySet()) {
			String fieldName = entry.getKey();
			if (!fieldName.equals(idName)) {
				Object fieldValue = entry.getValue().getValue();
				if (isNumeric(fieldValue.toString(),true)) {
					fieldValue = Float.valueOf(fieldValue.toString());
				}
				if (StringUtils.isNotBlank(prefix)
						&& fieldName.startsWith(prefix)) {
					fieldName = fieldName.substring(prefix.length());
				} else {
					onlyExternal = false;
				}
				model.put(fieldName, fieldValue);
			}
		}
	}

	/**
	 * 判断一个字符串是否是数字形式
	 * 
	 * @param str
	 *            String 待检测的字符串
	 * @param can_be_decimal
	 *            Boolean 可以是小数，即包含小数点的情况下也返回true
	 * @return boolean
	 */
	public static boolean isNumeric(String str, boolean hasDecimal) {
		boolean flag = true;
		int dotCount = 0; // 检查字符中小数点出现的次数，如果>=2，则不能认定这个字符串要表达是数字

		if (str == null || str.isEmpty()) {
			flag = false;
		} else {
			for (int i = str.length(); --i >= 0;) {
				if (!Character.isDigit(str.charAt(i))) {
					if (hasDecimal) {
						if (str.charAt(i) != '.')
							flag = false;
						else
							++dotCount; // 如果是小数点，计数器加1
					} else {
						flag = false;
					}
				}
			}
		}
		if (dotCount >= 2)
			flag = false;
		return flag;
	}

	public static boolean isNumeric(String str) {
		return str.matches("[\\+-]?[0-9]+((.)[0-9])*[0-9]*");
	}

	public boolean isOnlyExternal() {
		return onlyExternal;
	}
}
